var tableren;


function resta(){
    tableren.reload();
}

layui.use(['table','laydate','form','layer'],function (){
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    var layer = layui.layer;

    laydate.render({
        elem: '#js'
    });

    $(function (){
        tablein();
    })

    function tablein(){

        tableren =  table.render({

            elem:'#home',
            id:'tableIn',
            url:'/carousel/getAll',
            cellMinWidth: 80,
            page:{
                layout:['count','total','prev','page','next','limit'],
                limit:5,
                limits:[5,10,15],

            },
            cols:[
                [
                    {type: 'checkbox', fixed: 'left',}
                    ,{field: 'carouselId', title: '用户ID',algin:"right", width:150},
                    {field:'carouselimg',title:'轮播图图片',algin:'center', width: 180,
                        templet:function (d) {
                            return '<div><img src="http://43.143.176.39/group1/'+d.carouselImg+'"></div>'
                        }
                    },

             // return '<div>< img src="'+data.url+'", ' +
             //     'style="width: 60px;height: 60px"></div>'






                    {field:'carouselVideo',title:'轮播图视频',algin:'center', width: 250,
                        templet:function (data) {
                            return '<div id="openVideo"><video src="'+data.carouselVideo+'" style="width: 70%;height: 60%"></video></div>'
                        }
                    }
                    ,{field: 'createUser', title: '创建人', width:180}
                    ,{field: 'imgTitle', title: '图片标题', width:190}
                    // ,{field: 'imgType', title: '图片类型', width: 180}
                    ,{field: 'remarks', title: '备注', width: 190, sort: true}
                    ,{field: 'endDate', title: '结束时间', width: 190, sort: true}
                    ,{toolbar:'#bar',title: '操作',width:190}
                ]
            ],







            parseData:function (res){
                console.log(res)

                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
                console.log(data.carouselVideo);

            },
            done:function (res,curr,count){
                console.log(res);


                console.log(count);
            },
        });


        // $('#openVideo').click(function () {
        //     layer.open({
        //         type: 1,
        //         title: '播放视频',
        //         content: loadstr,
        //     });
        // });



        table.on('tool(carhome)',function (obj){

            var data = obj.data;
            var event = obj.event;
            if (event == 'edit'){
                console.log(data);
                layer.open({
                    type:2,
                    title:'修改',
                    content:"updatecarousel.html",
                    area:['80%','80%'],
                    success:function (layero,index){
                        let childFrame = layer.getChildFrame('body', index);
                        childFrame.find('input[name="carouselId"]').val(data.carouselId);
                        childFrame.find('input[name="imgTitle"]').val(data.imgTitle);
                        childFrame.find('input[name="display"]').val(data.display);
                        // childFrame.find('option[name="imgType"]').val(data.imgType);
                        childFrame.find('input[name="endDate"]').val(data.endDate);
                        childFrame.find('#demo1').attr("src",'http://47.98.150.96/group1/'+data.carouselImg);
                        childFrame.find('#pic').val(data.carouselImg);
                        childFrame.find('textarea[name="remarks"]').val(data.remarks);
                    }
                })
            }else if (event == 'delete'){
                layer.confirm('确定要下架嘛？', function(index) {
                    // console.log(data);
                    $.ajax({
                        url : "/carousel/del",
                        type : "get",
                        data : {"id" : data.carouselId}, // id与控制层传入的参数一样
                        dataType : "json",
                        success:function (){
                            // console.log(data.carouselId);
                            layer.close(index);
                            tablein();
                        }
                    });
                });
            }else if (event == 'openCarouselVideo'){
                layer.open({
                    type: 2,
                    title: '播放视频',
                    content: "carouselvideo.html",
                    area:['80%','80%'],
                    success:function(layero,index) {
                        var boby = layer.getChildFrame('body,index');

                        boby.find("td[name = carouselVideo]").html('<div><video src="'+obj.data.carouselVideo+'"style="width: 800px;height: 600px" controls></video> </div>');


                    }

                });
            }
        })
    }




    form.on('submit(search)',function (d){
        d = d.field;
        console.log(d.imgType)
        console.log(d.display)
        table.reload('tableIn',{
            where:{
                display:d.display,
                imgType:d.imgType
            }
        })
    })

    $('#refresh').click(function (){
        tablein();
    });

    $('#back').click(function (){
        layer.msg("请选择左侧导航栏进行返回")
    });

})